package xyz.xxin.xat.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import xyz.xxin.xat.R;
import xyz.xxin.xat.bean.ActivityDataBean;

public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> {
    private List<ActivityDataBean> activityDataBeans;
    private Context context;

    public HomeAdapter(List<ActivityDataBean> activityDataBeans, Context context) {
        this.activityDataBeans = activityDataBeans;
        this.context = context;
    }

    @NonNull
    @Override
    public HomeAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.activity_home_item, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull HomeAdapter.MyViewHolder holder, int position) {
        holder.activity_name.setText(activityDataBeans.get(position).getActivityName());
        holder.activity_item.setOnClickListener(view -> {
            if (itemClick != null){
                itemClick.OnItemClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return activityDataBeans.size();
    }

    private ItemClick itemClick;

    public void setItemClick(ItemClick itemClick) {
        this.itemClick = itemClick;
    }

    public interface ItemClick {
        void OnItemClick(int position);
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        private final LinearLayout activity_item;
        private final TextView activity_name;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            activity_item = itemView.findViewById(R.id.activity_item);
            activity_name = itemView.findViewById(R.id.activity_name);
        }
    }
}
